"use strict";
this.Test = function () {};

this.Test.prototype = (function () {
    var init, thread, benchmarkSpan, getModuleTest, setThreadPriorityTest,
            moduleEventTest, onModuleEventTest, redrawTest, onRedraw,
            eventTests, xPathTest, cssTest, jsonTest, getOffsetTest,
            getStyleTest, onResize;

    init = function (webLayout, that) {
        benchmarkSpan = webLayout.xPath('//p/span[@class="thread"]')[0];
        getModuleTest(webLayout, that);
        setThreadPriorityTest(webLayout);
        moduleEventTest(webLayout);
        redrawTest(webLayout);
        eventTests(webLayout);
        xPathTest(webLayout);
        cssTest(webLayout);
        jsonTest(webLayout);
        getOffsetTest(webLayout);
        getStyleTest(webLayout);
    };

    thread = function (webLayout) {
        benchmarkSpan.innerHTML = webLayout.threadPriority -
                webLayout.threadDelay;
        webLayout.css('.mouse')[0].innerHTML = webLayout.mouseX + ', ' +
                webLayout.mouseY;
    };

    onResize = function (webLayout, data) {
        webLayout.css('.resize')[0].innerHTML = data.width + '&times;' +
                data.height;
    };

    getStyleTest = function (webLayout) {
        var style = webLayout.getStyle(webLayout.css('p')[0]);
        if (style.borderWidth === undefined) {
            return;
        }
        webLayout.css('.getStyle')[0].innerHTML = 'OK';
    };

    getOffsetTest = function (webLayout) {
        var offset = webLayout.getOffset(webLayout.css('p')[0]);
        webLayout.css('.getOffset')[0].innerHTML = offset.left + ', ' +
                offset.top + ' - ' + offset.width + '&times;' + offset.height;
    };

    jsonTest = function (webLayout) {
        var result = webLayout.json("[\"ttt\", true, {\"a\": false}]");
        if ((result[0] !== 'ttt') || (result[1] !== true) ||
                (typeof result[2] !== 'object') || (result[2].a !== false)) {
            return;
        }
        webLayout.css('.json')[0].innerHTML = 'OK';
    };

    cssTest = function (webLayout) {
        webLayout.css('p span.css')[0].innerHTML = 'O';
        webLayout.css('.css')[0].innerHTML += 'K';
    };

    xPathTest = function (webLayout) {
        webLayout.xPath('p/span[@class="xPath"]')[0].innerHTML = 'OK';
    };

    eventTests = function (webLayout) {
        var check = false, element = webLayout.css('.event')[0], handler;
        handler = function () {
            check = check === 'OK' ? 'failed' : 'OK';
        };
        webLayout.addEventListener(element, 'click', handler);
        webLayout.dispatchEvent(element, 'click');
        webLayout.removeEventListener(element, 'click', handler);
        webLayout.dispatchEvent(element, 'click');
        element.innerHTML = check;
        element = this.document.createElement('a');
        element.href = 'http://www.google.com/';
        webLayout.addEventListener(element, 'click', function (e) {
            webLayout.preventDefault(e);
        });
        element.innerHTML = 'if this link works, events does not';
        webLayout.css('.event')[0].appendChild(element);
    };

    onRedraw = function (webLayout, span) {
        span.innerHTML = 'OK';
    };

    redrawTest = function (webLayout) {
        webLayout.redraw(webLayout.xPath('p/span[@class="redraw"]')[0]);
    };

    onModuleEventTest = function (webLayot, span) {
        span.innerHTML = 'OK';
    };

    moduleEventTest = function (webLayout) {
        webLayout.moduleEvent('moduleEventTest',
                webLayout.xPath('p/span[@class="moduleEvent"]')[0]);
    };

    setThreadPriorityTest = function (webLayout) {
        webLayout.setThreadPriority(500);
    };

    getModuleTest = function (webLayout, that) {
        var span = webLayout.xPath('p/span[@class="getModule"]')[0];
        span.innerHTML = webLayout.getModule('Test') === that ? 'OK' : 'fail';
    };

    return {
        NAME: 'Test',
        init: function (webLayout) {
            init(webLayout, this);
        },
        thread: thread,
        onModuleEventTest: onModuleEventTest,
        onResize: onResize,
        onRedraw: onRedraw
    };
}());